//
//  ERPListView.swift
//  ERP
//
//  Created by ifly on 2017/8/12.
//  Copyright © 2017年 szy. All rights reserved.
//

import UIKit

class ERPListView: UIView ,UITableViewDataSource,UITableViewDelegate{

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    private var rootView = UITableView()
    private var selectIndexPath:IndexPath = [0,0]
    private var dataArray = NSArray()
    private var sectlorModel = ListModel()
    private var sectArray = NSMutableArray()
    private var sectDataArray = NSMutableArray()
    private var isShow = Bool()
    private var viewH = CGFloat()
    private var backBtn = UIButton()
    

    
    var isMoreSelect:Bool?
    
    var kkListViewDidSelect:((_ listString:ListModel) -> Void)?
    var kkListViewDidSelectArray:((_ listString:NSMutableArray) -> Void)?
    var kkListViewCellText:((_ cellIndex:IndexPath) -> String)?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
     init(viewHeight:CGFloat) {
        
        
        super.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: viewHeight))
        
        self.viewH = viewHeight
        
        self.backgroundColor = UIColor.gray
        
        self.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: viewHeight)
        
        let btn = UIButton(type: .custom)
        btn.setTitle("完成", for: .normal)
        btn.frame = CGRect.init(x: self.frame.size.width - 60, y: 0, width: 60, height: 40)
        self.addSubview(btn)
        btn.setTitleColor(UIColor.blue, for: .normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        btn.addTarget(self, action: #selector(btnAction), for: UIControlEvents.touchUpInside)
        
        
        rootView = UITableView.init(frame: CGRect.init(x: 0, y: 40, width: self.frame.size.width, height: viewHeight - 35), style: UITableViewStyle.plain)
        rootView.delegate = self
        rootView.dataSource = self
        self.addSubview(rootView)
        rootView.tableFooterView = UIView()
        rootView.rowHeight = 40;
        
    }
    
    func setDataArray(_ dArray:NSArray) -> Void {
        
        self.dataArray = dArray
        sectlorModel = self.dataArray[0] as! ListModel
        self.rootView.reloadData()
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellid = "ERPAddProTableViewCell"
        var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellid)
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: cellid)
        }
        
        
        cell?.selectionStyle = .none
        
        
        if self.kkListViewCellText != nil{
            cell?.textLabel?.text = self.kkListViewCellText!(indexPath)
        }else{
            
            let model=self.dataArray[indexPath.row] as! ListModel
            
            cell?.textLabel?.text = model.name
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        
        if self.isMoreSelect == true {
            
            cell?.accessoryType = .none
            for  index in self.sectArray {
                
                if indexPath == index as! IndexPath {
                    cell?.accessoryType = .checkmark
                }
            }
            
            
        }else{
            
            if selectIndexPath == indexPath {
                cell?.accessoryType = .checkmark
            }else{
                cell?.accessoryType = .none
            }

        }
        
        
        return cell!
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        if self.isMoreSelect == true{
            
            let cell:UITableViewCell = tableView.cellForRow(at: indexPath)!
            if cell.accessoryType == .checkmark{
                cell.accessoryType = .none
                self.sectArray.remove(indexPath)
                
                
                let model = self.dataArray[indexPath.row] as! ListModel
                self.sectDataArray.remove(model)
                
            }else{
                cell.accessoryType = .checkmark
                self.sectArray.add(indexPath)
                
                let model = self.dataArray[indexPath.row] as! ListModel
                self.sectDataArray.add(model)
                
            }
            
        }else{
            
            print(selectIndexPath)
            print(indexPath)
            

            
            let selectCell:UITableViewCell = tableView.cellForRow(at: selectIndexPath)!
            selectCell.accessoryType = .none
            
            selectIndexPath = indexPath
            
            let cell:UITableViewCell = tableView.cellForRow(at: indexPath)!
            cell.accessoryType = .checkmark
            
            sectlorModel = self.dataArray[indexPath.row] as! ListModel

        }

        
    }
    
    
    @objc func btnAction() -> Void {
        
        if self.isMoreSelect == true {
            
            if self.kkListViewDidSelectArray != nil {
                self.kkListViewDidSelectArray!(self.sectDataArray)
            }
        }else{
            
            if self.kkListViewDidSelect != nil {
                self.kkListViewDidSelect!(sectlorModel)
            }

        }
        
        if self.isShow == true {
            dismiss()
        }


    }
    
    
    func show() -> Void {
        
        self.isShow = true
        
        backBtn = UIButton(type: .custom)
        backBtn.backgroundColor = UIColor.clear
        backBtn.addTarget(self, action: #selector(ERPListView.dismiss), for: .touchUpInside)
        let wind = UIApplication.shared.keyWindow
        backBtn.frame = (wind?.frame)!
        wind?.addSubview(backBtn)
        
        backBtn.addSubview(self)
        self.frame = CGRect.init(x: 0, y: UIScreen.main.bounds.size.height - viewH, width: UIScreen.main.bounds.size.width, height: viewH)
        
    }
    
    @objc func dismiss() -> Void {
        
        backBtn.removeFromSuperview()
        
    }
    
}

class ListModel: NSObject {
    var name:String?
    var id:String?
    var IDCard:String?
    var EmpGuid:String?
    var CorpCode:String?

}

